package com.example.fengqiqi

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton

data class Student(
    val id: String,
    val name: String,
    val gender: String,
    val className: String
)


class MainActivity : AppCompatActivity() {
    private lateinit var fileHelper: FileHelper
    private lateinit var studentAdapter: StudentAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        fileHelper = FileHelper(this)
        val students = fileHelper.loadStudents()

        studentAdapter = StudentAdapter(students)
        val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = studentAdapter

        val fab: FloatingActionButton = findViewById(R.id.fab)
        fab.setOnClickListener {
            val intent = Intent(this, AddStudentActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        val students = fileHelper.loadStudents()
        studentAdapter = StudentAdapter(students)
        val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
        recyclerView.adapter = studentAdapter
    }
}